package com.safe.text;

import com.safe.text.R;
import com.markupartist.android.widget.ActionBar;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class SettingsActivity extends BaseActivity {
	EditText phoneNumber;
	EditText emailAddress;
	EditText password;
	TextView installDate;
	Button setNumber;
	Button setEmail;
	Button setPassword;
	CheckBox startup;
	CheckBox smsPref;
	ImageView emailDownArrow;
	ImageView phoneDownArrow;
	ImageView passwordDownArrow;
	ImageView emailRightArrow;
	ImageView phoneRightArrow;
	ImageView passwordRightArrow;
	Boolean gpsActive = false;
	RelativeLayout rlEmail;
	RelativeLayout rlPhone;
	RelativeLayout rlPassword;
	static ToggleButton serviceToggle;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		final Intent mainService = new Intent(SettingsActivity.this,
				MainService.class);
		if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			gpsActive = true;
		}
		findViewById(R.id.layout_phone).setVisibility(View.GONE);
		findViewById(R.id.layout_email).setVisibility(View.GONE);
		findViewById(R.id.layout_password).setVisibility(View.GONE);
		currentActivity = this;
		final SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		final SharedPreferences.Editor prefsEditor = prefs.edit();
		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("Settings");
		actionBar.setHomeAction(new HomeAction());
		actionBar.addAction(new AboutAction());
		actionBar.addAction(new SettingsAction());
		actionBar.addAction(new RefreshAction());
		serviceToggle = (ToggleButton) findViewById(R.id.toggleService);
		// Initialize Toggle Button
		if (isMyServiceRunning()) {
			serviceToggle.setChecked(true);
			// serviceStatus.setText("Service Started");
		} else {
			serviceToggle.setChecked(false);
		}

		installDate = (TextView) findViewById(R.id.tv_install_date);
		phoneNumber = (EditText) findViewById(R.id.etv_phone);
		emailAddress = (EditText) findViewById(R.id.etv_email);
		password = (EditText) findViewById(R.id.etv_password_settings);
		setNumber = (Button) findViewById(R.id.btn_saveNumber);
		setEmail = (Button) findViewById(R.id.btn_saveEmail);
		setPassword = (Button) findViewById(R.id.btn_savePassword);
		startup = (CheckBox) findViewById(R.id.cb_startup);
		smsPref = (CheckBox) findViewById(R.id.cb_smsPref);
		emailDownArrow = (ImageView) findViewById(R.id.iv_email_down);
		phoneDownArrow = (ImageView) findViewById(R.id.iv_phone_down);
		passwordDownArrow = (ImageView) findViewById(R.id.iv_password_down);
		emailRightArrow = (ImageView) findViewById(R.id.iv_email_right);
		phoneRightArrow = (ImageView) findViewById(R.id.iv_phone_right);
		passwordRightArrow = (ImageView) findViewById(R.id.iv_password_right);
		rlEmail = (RelativeLayout) findViewById(R.id.rl_expand_email);
		rlPhone = (RelativeLayout) findViewById(R.id.rl_expand_phone);
		rlPassword = (RelativeLayout) findViewById(R.id.rl_expand_password);

		// Populate Fields
		if (prefs.getBoolean(KEY_START_ON_BOOT, false)) {
			startup.setChecked(true);
		} else {
			startup.setChecked(false);
		}
		// Populate Fields
		if (prefs.getBoolean(KEY_SMS_PREF, false)) {
			smsPref.setChecked(true);
		} else {
			smsPref.setChecked(false);
		}
		BaseActivity.phoneNumber = prefs.getString("phoneNumber", "");
		BaseActivity.emailAddress = prefs.getString("emailAddress", "");
		BaseActivity.password = prefs.getString("password", "");
		installDate.setText(getDate(prefs.getLong(KEY_FIRST_RUN, 0),
				"MM/dd/yyyy h:mm:ss aa"));
		if (BaseActivity.phoneNumber != "") {
			phoneNumber.setText(prefs.getString("phoneNumber", ""));
		}
		if (BaseActivity.emailAddress != "") {
			emailAddress.setText(prefs.getString("emailAddress", ""));
		}
		if (BaseActivity.password != "") {
			password.setText(prefs.getString("password", ""));
		}
		rlEmail.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (findViewById(R.id.layout_email).getVisibility() == View.GONE) {
					emailRightArrow.setVisibility(View.GONE);
					emailDownArrow.setVisibility(View.VISIBLE);
					phoneRightArrow.setVisibility(View.VISIBLE);
					phoneDownArrow.setVisibility(View.GONE);
					passwordRightArrow.setVisibility(View.VISIBLE);
					passwordDownArrow.setVisibility(View.GONE);
					findViewById(R.id.layout_password).setVisibility(View.GONE);
					findViewById(R.id.layout_phone).setVisibility(View.GONE);
					findViewById(R.id.layout_email).setVisibility(View.VISIBLE);
					// mgr.showSoftInput(emailAddress,InputMethodManager.SHOW_FORCED);
				} else {
					emailRightArrow.setVisibility(View.VISIBLE);
					emailDownArrow.setVisibility(View.GONE);
					findViewById(R.id.layout_email).setVisibility(View.GONE);
					mgr.hideSoftInputFromWindow(emailAddress.getWindowToken(),
							0);
				}

			}
		});
		rlPhone.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (findViewById(R.id.layout_phone).getVisibility() == View.GONE) {
					phoneRightArrow.setVisibility(View.GONE);
					phoneDownArrow.setVisibility(View.VISIBLE);
					emailRightArrow.setVisibility(View.VISIBLE);
					emailDownArrow.setVisibility(View.GONE);
					passwordRightArrow.setVisibility(View.VISIBLE);
					passwordDownArrow.setVisibility(View.GONE);
					findViewById(R.id.layout_password).setVisibility(View.GONE);
					findViewById(R.id.layout_email).setVisibility(View.GONE);
					findViewById(R.id.layout_phone).setVisibility(View.VISIBLE);
					// mgr.showSoftInput(phoneNumber,InputMethodManager.SHOW_FORCED);
				} else {
					phoneRightArrow.setVisibility(View.VISIBLE);
					phoneDownArrow.setVisibility(View.GONE);
					findViewById(R.id.layout_phone).setVisibility(View.GONE);
					mgr.hideSoftInputFromWindow(phoneNumber.getWindowToken(), 0);
				}

			}
		});
		rlPassword.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (findViewById(R.id.layout_password).getVisibility() == View.GONE) {
					passwordRightArrow.setVisibility(View.GONE);
					passwordDownArrow.setVisibility(View.VISIBLE);
					emailRightArrow.setVisibility(View.VISIBLE);
					emailDownArrow.setVisibility(View.GONE);
					phoneRightArrow.setVisibility(View.VISIBLE);
					phoneDownArrow.setVisibility(View.GONE);
					findViewById(R.id.layout_email).setVisibility(View.GONE);
					findViewById(R.id.layout_phone).setVisibility(View.GONE);
					findViewById(R.id.layout_password).setVisibility(
							View.VISIBLE);
					// mgr.showSoftInput(password,
					// InputMethodManager.SHOW_FORCED);
				} else {
					passwordRightArrow.setVisibility(View.VISIBLE);
					passwordDownArrow.setVisibility(View.GONE);
					findViewById(R.id.layout_password).setVisibility(View.GONE);
					mgr.hideSoftInputFromWindow(password.getWindowToken(), 0);
				}

			}
		});
		setNumber.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ParsePhoneUpdate(prefs.getString("emailAddress", ""), prefs
						.getString("password", ""), phoneNumber.getText()
						.toString());
				mgr.hideSoftInputFromWindow(phoneNumber.getWindowToken(), 0);
			}
		});
		setEmail.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (isEmailFormatValid(emailAddress.getText().toString())){
				ParseEmailUpdate(prefs.getString("emailAddress", ""), prefs
						.getString("password", ""), emailAddress.getText()
						.toString());
				mgr.hideSoftInputFromWindow(emailAddress.getWindowToken(), 0);
				}
			}
		});
		setPassword.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (isPasswordFormatValid(password.length())){
				ParsePasswordUpdate(prefs.getString("emailAddress", ""), prefs
						.getString("password", ""), password.getText()
						.toString());
				mgr.hideSoftInputFromWindow(password.getWindowToken(), 0);
				}
			}
		});
		startup.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (((CheckBox) v).isChecked()) {
					prefsEditor.putBoolean(KEY_START_ON_BOOT, true);
					ParseAppStart(prefs.getString("emailAddress", ""),
							prefs.getString("password", ""), true);
				} else {
					prefsEditor.putBoolean(KEY_START_ON_BOOT, false);
					ParseAppStart(prefs.getString("emailAddress", ""),
							prefs.getString("password", ""), false);
				}
				prefsEditor.commit();
			}
		});
		smsPref.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (((CheckBox) v).isChecked()) {
					prefsEditor.putBoolean(KEY_SMS_PREF, true);
					ParseSMSPref(prefs.getString("emailAddress", ""),
							prefs.getString("password", ""), true);
				} else {
					prefsEditor.putBoolean(KEY_SMS_PREF, false);
					ParseSMSPref(prefs.getString("emailAddress", ""),
							prefs.getString("password", ""), false);
				}
				prefsEditor.commit();
			}
		});
		// registers the service that checks for speed
		serviceToggle
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						Log.e("ischecked", "" + isChecked);
						if (isChecked) {
							// The toggle is enabled
							if (started == false) {
								// register the main service that handles the
								// location and sms booleans
								if (!manager
										.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
									GPSAlert();

									gpsActive = false;
									serviceToggle.setChecked(false);
								} else {
									if (parseIsRunning == false){
									started = true;
									gpsActive = true;
									startService(mainService);
									parseIsRunning = true;
									ParseAppOn(
											prefs.getString("emailAddress", ""),
											prefs.getString("password", ""),
											true);
									} else {
										serviceToggle.setChecked(false);
									}
									// serviceStatus.setText("Service Started");
								}

							} else {
								if (!isMyServiceRunning()) {
									if (gpsActive == false) {

									} else {
										if (parseIsRunning == false){
										started = false;
										stopService(mainService);
										parseIsRunning = true;
										ParseAppOn(prefs.getString(
												"emailAddress", ""), prefs
												.getString("password", ""),
												false);
									}
										else {
											serviceToggle.setChecked(true);
										}
									}
								}
								// serviceStatus
								// .setText("Service already Started");
							}
						} else {
							// The toggle is disabled
							// register the main service that handles the
							// location and sms booleans
							if (parseIsRunning == false){
							started = false;
							stopService(mainService);
							parseIsRunning = true;
							ParseAppOn(prefs.getString("emailAddress", ""),
									prefs.getString("password", ""), false);
						}
						else {
							serviceToggle.setChecked(true);
						}
							// serviceStatus.setText("Service Stopped");

						}
					}
				});
	}

	public void onResume() {
		currentActivity = this;
		super.onResume();
	}

}
